

import 'package:flutter/material.dart';
import 'package:marquee/marquee.dart';


/// 自动滚动的文本组件，当文本长度超过控件长度时，自动滚动，未超出不会滚动
class AutoMarquee extends StatefulWidget{
  final String value;
  final TextStyle style;
  final double speed;


  const AutoMarquee({super.key, required this.value, required this.style , this.speed = 30});
  @override
  State<StatefulWidget> createState()  => _AutoMarqueeState();

}

class _AutoMarqueeState extends State<AutoMarquee> {
  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (BuildContext context, BoxConstraints constraints) {
        final TextPainter textPainter = TextPainter(
          text: TextSpan(
            text: widget.value,
            style: widget.style,
          ),
          maxLines: 1,
          textDirection: TextDirection.ltr,
        )..layout(minWidth: 0, maxWidth: constraints.maxWidth);

        if (textPainter.didExceedMaxLines) {
          return SizedBox(
            height: constraints.maxHeight,
            child: Marquee(
              text: widget.value,
              style: widget.style,
              velocity: widget.speed,
              blankSpace: 20,
              fadingEdgeStartFraction: 0.2,
              fadingEdgeEndFraction: 0.2,
            ),
          );
        } else {
          return Text(widget.value , style: widget.style,);
        }
      },
    );
  }
}